/**
 * 
 */
package com.xijing.view;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.table.DefaultTableModel;

import com.xijing.model.*;
import com.xijing.service.AdminService;
import com.xijing.util.RendedTable;


public class AdminFrame extends JDialog implements ActionListener, ListSelectionListener {

	private static final long serialVersionUID = 1L;

	private List<SYS_User> admList = new ArrayList<SYS_User>(); 
	private AdminService adminService = new AdminService();
	
	private JPanel jContentPane = null;
	private JScrollPane tableScrollPane = null;
	private RendedTable admListTable = null;
	private String[][] tableData = null;
	private DefaultTableModel model = null;
	private JButton btn_select = null;

	/**
	 * This method initializes
	 * 
	 */
	public AdminFrame(JFrame f,Boolean b) {
		super(f,"用户信息管理",b);
		initialize();
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
		this.setBounds(new Rectangle(300, 80, 600, 400));
		this.setContentPane(getJContentPane());
		this.setTitle("管理员管理界面");
		this.setResizable(false);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	}

	/**
	 * This method initializes jContentPane
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJContentPane() {
		if (jContentPane == null) {
			jContentPane = new JPanel();
			jContentPane.setLayout(null);
			jContentPane.add(getTableScrollPane());
			jContentPane.add(getBtn_select());
		}
		return jContentPane;
	}

	/**
	 * This method initializes tableScrollPane
	 * 
	 * @return javax.swing.JScrollPane
	 */
	private JScrollPane getTableScrollPane() {
		if (tableScrollPane == null) {
			tableScrollPane = new JScrollPane();
			tableScrollPane.setBounds(new Rectangle(5, 5, 576, 320));
			tableScrollPane.setViewportView(getAdmListTable());
		}
		return tableScrollPane;
	}

	/**
	 * This method initializes contractListPanel
	 * 
	 * @return javax.swing.RendedTable
	 */
	private RendedTable getAdmListTable() {
		if (admListTable == null) {
			String[] header = { "管理员编号", "姓名", "状态" };
			model = new DefaultTableModel(tableData, header) {
				private static final long serialVersionUID = 1L;

				public int getColumnCount() {
					return 3;
				}

				public boolean isCellEditable(int row, int column) {
					return true;
				}
			};

			admListTable = new RendedTable(model);
			admListTable.setShowGrid(true);
			admListTable.setSelectionForeground(Color.RED);

			admListTable.setBackground(Color.LIGHT_GRAY);
			admListTable.setMinimumSize(new Dimension(150, 80));
			admListTable.setAutoResizeMode(RendedTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
			admListTable.setBounds(new Rectangle(0, 0, 800, 80));
			admListTable.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

			ListSelectionModel lst = this.admListTable.getSelectionModel();
			lst.addListSelectionListener(this);
		}
		return admListTable;
	}

	/**
	 * This method initializes btn_select
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getBtn_select() {
		if (btn_select == null) {
			btn_select = new JButton();
			btn_select.setBounds(new Rectangle(488, 335, 93, 26));
			btn_select.setText("查  询");
			btn_select.addActionListener(this);
		}
		return btn_select;
	}

	public void display(List<SYS_User> admList) {
		if (getAdmListTable() != null) {
			// 清空当前表格的所有值
			model.setRowCount(0);
			this.admList = admList;
			tableData = new String[this.admList.size()][model.getColumnCount()];
			for (int i = 0; i < this.admList.size(); i++) {
				tableData[i][0] = String.valueOf(this.admList.get(i).getSU_ID());
				tableData[i][1] = this.admList.get(i).getSU_RealName();
				switch (this.admList.get(i).getSU_State()) {
				case 1:
					tableData[i][2] = "正常";
					break;
				case 2:
					tableData[i][2] = "锁定";
					break;
				case 3:
					tableData[i][2] = "删除";
					break;
				default:
					tableData[i][2] = "-";
				}
				
				model.addRow(tableData[i]);
			}
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		//查询按钮
		if (e.getSource() == this.btn_select) {
			List<SYS_User> admList = adminService.search();
			this.display(admList);
		}
	}

	@Override
	public void valueChanged(ListSelectionEvent e) {
	}

} // @jve:decl-index=0:visual-constraint="10,10"
